package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import java.util.StringTokenizer;

import model.ContactUser;
import model.OnomasticoI;


public class StubClient implements OnomasticoI{
	
	
	public StubClient(){
		//attivo i flussi di I/O
		try {
			clientSocket= new Socket("localhost", porto);
			input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
			output = new PrintStream(clientSocket.getOutputStream());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	//attributi
	private Socket clientSocket;
	private BufferedReader input;
	private PrintStream output;
	private int porto=9001;
	@Override
	public String selectByName(String nome) {
		//inviata la richiesta lato server
		output.println(nome);
		output.flush();
		String receive="";
		try {
			receive = input.readLine();
			System.out.println(receive);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return receive;
	}
	@Override
	public List<ContactUser> getAllUserName() {
		// TODO Auto-generated method stub
		return null;
	}
	
	private static String calcolaMese(int mese) {
		String meseCalcolato = "";

		switch (mese) {
		case 1:
			meseCalcolato = "Gennaio";
			break;
		case 2:
			meseCalcolato = "Febbraio";
			break;
		case 3:
			meseCalcolato = "Marzo";
			break;
		case 4:
			meseCalcolato = "Aprile";
			break;
		case 5:
			meseCalcolato = "Maggio";
			break;
		case 6:
			meseCalcolato = "Giugno";
			break;
		case 7:
			meseCalcolato = "Luglio";
			break;
		case 8:
			meseCalcolato = "Agosto";
			break;
		case 9:
			meseCalcolato = "Settembre";
			break;
		case 10:
			meseCalcolato = "Ottobre";
			break;
		case 11:
			meseCalcolato = "Novembre";
			break;
		case 12:
			meseCalcolato = "Dicembre";
			break;
		}

		return meseCalcolato;
	}
	
	public static void main(String args[]){
		
		String nome="Salvatore";
		StubClient stub = new StubClient();
		stub.selectByName(nome);
//		ContactUser user = new ContactUser();
//		
//		String date = "18-08";
//		StringTokenizer str = new StringTokenizer(date, "-");
//		StubClient stub = new StubClient();
//		user = stub.selectByDate(date);
//		int giorno = Integer.parseInt(str.nextToken());
//		int mese = Integer.parseInt(str.nextToken());
//		System.out.println("Nel giorno: " + giorno +" del mese di : "+calcolaMese(mese) + " " +
//				" si festeggia l'onomastico di : "+ user.getNome());
	}
	@Override
	public ContactUser selectByDate(String date) {
		
		ContactUser user = new ContactUser();
		output.println(date);
		output.flush();
		String receive="";
		try {
			receive = input.readLine();
			user.setNome(receive);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
}
